Adding some more judges, here and there.
[andmenj-acm.git] / UVa / 10199 - Tourist guide / Intentando ayudar / 10199.cpp
blobbee95a52b0293f776b8bce70a0518a85fb6512b8
1 #include<stdio.h>
2 #include<string.h>
3 #define MAX 106
5 #define WHITE 0
6 #define GRAY 1
7 #define BLACK 2
9 int a[MAX][MAX], n;
10 int color[MAX], d[MAX], low[MAX], pre[MAX], used[MAX];
12 char AP[MAX][33];
13 int apN;
15 char city[MAX][33];
17 int time;
19 void init()
21 int i, j;
22 for(i=0; i<n; i++)
24 color[i]=0;
25 pre[i]=-1;
26 used[i]=0;
28 for(i=0; i<n; i++)
29 for(j=0; j<n; j++)
30 a[i][j]=0;
33 void DFS_Visit(int u)
35 color[u] = GRAY;
36 time = time+1;
37 d[u] = time;
38 low[u]=time;
39 int gr=-1, i, fl=0;
40 for(i=0; i<n; i++)
41 if(a[u][i]==1)
43 if(color[i] == WHITE)
45 pre[i]=u;
46 a[i][u]=0;
47 DFS_Visit(i);
49 fl++;
50 if(pre[u]==-1)
52 if(fl==2 && used[u]==0)
54 used[u]=1;
55 strcpy(AP[apN], city[u]);
56 apN++;
57 continue;
60 else if(low[i]>=d[u] && used[u]==0)
62 used[u]=1;
63 strcpy(AP[apN], city[u]);
64 apN++;
66 else if(low[i]<low[u])
68 low[u]=low[i];
71 else if(color[i]==GRAY)
73 if(d[i]<low[u])
74 low[u]=d[i];
76 else if(color[i]==BLACK)
78 if(low[i]<low[u])
79 low[u]=low[i];
83 color[u] = BLACK;
84 time = time+1;
88 int fndL(char *s)
90 int i;
91 for(i=0; i<n; i++)
92 if(strcmp(city[i], s)==0)
93 return i;
97 int main()
99 //freopen("in.txt", "r", stdin);
100 int test=0, i, j, x, y,m, v;
101 char s1[33], s2[33], tmp[33];
103 while(scanf("%d", &n)==1 && n)
105 for(i=0; i<n; i++)
106 scanf("%s", city[i]);
108 scanf("%d", &m);
109 init();
110 for(i=0; i<m; i++)
112 scanf("%s %s", &s1, &s2);
113 x=fndL(s1);
114 y=fndL(s2);
115 a[x][y]=1;
116 a[y][x]=1;
118 apN=0;
119 time=0;
120 for(i=0; i<n; i++)
121 DFS_Visit(i);
122 if(test)
123 puts("");
124 printf("City map #%d: %d camera(s) found\n", test+1, apN);
125 test++;
126 for(i=0; i<apN; i++)
127 for(j=i+1; j<apN; j++)
128 if(strcmp(AP[i], AP[j])>0)
130 strcpy(tmp, AP[i]);
131 strcpy(AP[i], AP[j]);
132 strcpy(AP[j], tmp);
134 for(i=0; i<apN; i++)
135 printf("%s\n", AP[i]);
138 return 0;